<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2023 yoncron All rights reserved.
// +----------------------------------------------------------------------
// | Author: yoncron <yoncron@163.com>
// +----------------------------------------------------------------------
namespace plugin\webman\gateway;

use App\model\UserModel;
use GatewayWorker\Lib\Gateway;

class Events
{
    public static function onWorkerStart($worker)
    {
        UserModel::where('online_status','=','online')->update(['online_status'=>'offline']);
    }

    public static function onConnect($client_id)
    {

    }

    public static function onWebSocketConnect($client_id, $data)
    {

    }

    public static function onMessage($client_id, $data)
    {
        $_user_id = Gateway::getUidByClientId($client_id);
        $message = json_decode($data, true);
        $message_type = $message['type'];
        switch($message_type) {
            case 'init':
                // 通知当前客户端初始化
                $init_message = array(
                    'message_type' => 'init',
                    'client_id' => $client_id,
                );
                Gateway::sendToClient($client_id, json_encode($init_message));
                return;
            case 'online':
                $status_message = array(
                    'message_type' => $message_type,
                    'id' => $_user_id,
                );
                Gateway::sendToAll(json_encode($status_message));
                return;
            case 'ping':
                $pong_message = array(
                    'message_type' => 'pong',
                    'client_id'    => $client_id,
                    'timestamp'    => time()
                );
                Gateway::sendToClient($client_id, json_encode($pong_message));
                self::ping($client_id,$message['platform']);
                return;
            case 'console':
                $test_message = array(
                    'message_type' => 'console',
                    'content'    => "content to server success.",
                    'timestamp'    => strval(date("Y-m-d H:i:s", time()))
                );
                Gateway::joinGroup($client_id,'dev');
                Gateway::sendToClient($client_id, json_encode($test_message));
                return;
            case 'error':
                $test_message = array(
                    'message_type' => 'error',
                    'content'    => "content to server success.",
                    'timestamp'    => strval(date("Y-m-d H:i:s", time()))
                );
                Gateway::joinGroup($client_id,'dev');
                Gateway::sendToClient($client_id, json_encode($test_message));
                return;
//            case 'scanToLogin':
//                $test_message = array(
//                    'message_type' => 'login_sucess',
//                    'access_token' => $message['access_token'],
//                    'timestamp' => time()
//                );
//                Gateway::sendToClient($message['client_id'], json_encode($test_message));
//                return;
            default:
                $error_message = array(
                    'message' => 'unknown message'
                );
                Gateway::sendToClient($client_id, json_encode($error_message));
        }
    }

    public static function onClose($client_id)
    {
        $userObj = new UserModel();
        $user_id = Gateway::getUidByClientId($client_id);

        //$logout_message = array('message_type' => 'logout', 'id' => $user_id);
        //Gateway::sendToAll(json_encode($logout_message));

        $userObj->where(['user_id'=>$user_id])->update(['logout_time'=>time(),'online_status'=>'offline']);
    }

    public static function onWorkerStop($worker)
    {
        UserModel::where('online_status','=','online')->update(['online_status'=>'offline']);
    }

    public static function ping($client_id,$platform){
        $user_id = Gateway::getUidByClientId($client_id);
        $userObj = new UserModel();
        $userObj->where(['user_id'=>$user_id])->update(['last_login_time'=>time(),'online_status'=>'online']);

    }
}